package com.usian.controller;

import com.usian.config.ResponseResult;
import com.usian.mapper.SupplyMapper;
import com.usian.pojo.Product;
import com.usian.pojo.Supply;
import com.usian.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private SupplyMapper supplyMapper;
    @RequestMapping("/findAll")
    public List<Product> findAll(@RequestBody Product product){
return productService.findAll(product);
    }
    @RequestMapping("/findAllss")
    public List<Supply> findAllss(){
        return supplyMapper.selectList(null);
    }
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody Product product){
        try {
            product.setTime(new Date());
            productService.save(product);
            return ResponseResult.errorResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"添加失败");
        }
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Product product){
        try {
            product.setTime(new Date());
            productService.updateById(product);
            return ResponseResult.errorResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"修改失败");
        }
    }
    @PostMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Integer id){
        try {
            productService.removeById(id);
            return ResponseResult.errorResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"删除失败");
        }
    }
    @GetMapping("/findByid")
    public Product findByid(@RequestParam("id") Integer id){
        return productService.findByid(id);
    }
}
